package com.duoec.dw.lineage.service.analyser.field;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.duoec.dw.lineage.dto.TableFieldLineageDto;
import com.duoec.dw.lineage.service.analyser.HiveSelectFieldAnalyser;
import com.duoec.dw.lineage.service.HiveAnalyser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashSet;

/**
 * @author xuwenzhen
 */
public abstract class BaseFieldAnalyser<T extends SQLExpr> implements HiveSelectFieldAnalyser<T> {
    @Autowired
    protected HiveAnalyser hiveAnalyser;

    public void setHiveAnalyser(HiveAnalyser hiveAnalyser) {
        this.hiveAnalyser = hiveAnalyser;
    }

    protected LinkedHashSet<TableFieldLineageDto> mergeFieldLineages(LinkedHashSet<TableFieldLineageDto> existsFields, LinkedHashSet<TableFieldLineageDto> newFields) {
        LinkedHashSet<TableFieldLineageDto> fields = new LinkedHashSet();
        newFields.forEach(field -> {
            TableFieldLineageDto dto = new TableFieldLineageDto();
            BeanUtils.copyProperties(field, dto);
            dto.setIndex(existsFields.size());

            existsFields.add(dto);
            fields.add(dto);
        });
        return fields;
    }
}
